/*package com.fbb.widget;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.fbb.tet.R;

public class PullToRefreshListView extends ListView implements OnScrollListener {
	private final static int RELEASE_To_REFRESH = 0;
	private final static int PULL_To_REFRESH = 1;
	private final static int REFRESHING = 2;
	private final static int DONE = 3;

	private LayoutInflater inflater;
	private LinearLayout headView;
	private TextView tipsTextview;
	private TextView lastUpdatedTextView;
	private ImageView arrowImageView;
	private ProgressBar progressBar;

	private RotateAnimation animation;
	private RotateAnimation reverseAnimation;

	// 用于保证startY的值在一个完整的touch事件中只被记录一次
	private boolean isRecored;

	private int headContentHeight;
	private int startY;
	private int state;

	private boolean isBack;
	private boolean isFront;

	public OnRefreshListener refreshListener;
	public OnPageDownListener pageDownListener;

	private boolean tagHead = false;

	private LinearLayout footerView;
	private LinearLayout linearLayoutProgress;
	private TextView textViewFooter;
	
//	private Thread animationThread;
	private Activity activity;
	private int footContentHeight;
//	private int footContentWidth;
	private int pullMood = 0;
	public int PULL_MOOD_HEAD = 1;
	public int PULL_MOOD_FOOT = 2;
	private boolean tagFoot = false;
	private boolean isRefresh = false;

	public PullToRefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {
		inflater = LayoutInflater.from(context);

		headView = (LinearLayout) inflater.inflate(R.layout.pull_to_refresh_head, null);
		arrowImageView = (ImageView) headView.findViewById(R.id.head_arrowImageView);
		arrowImageView.setMinimumWidth(50);
		arrowImageView.setMinimumHeight(50);
		progressBar = (ProgressBar) headView.findViewById(R.id.head_progressBar);
		tipsTextview = (TextView) headView.findViewById(R.id.head_tipsTextView);
		lastUpdatedTextView = (TextView) headView.findViewById(R.id.head_lastUpdatedTextView);

		measureView(headView);
		headContentHeight = headView.getMeasuredHeight();
//		headContentWidth = headView.getMeasuredWidth();

		headView.setPadding(0, -1 * headContentHeight, 0, 0);
		headView.invalidate();

		addHeaderView(headView);
		setOnScrollListener(this);

		animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		animation.setInterpolator(new LinearInterpolator());
		animation.setDuration(250);
		animation.setFillAfter(true);

		reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		reverseAnimation.setInterpolator(new LinearInterpolator());
		reverseAnimation.setDuration(250);
		reverseAnimation.setFillAfter(true);
		
		footerView = (LinearLayout) inflater.inflate(R.layout.list_footer, null);
		measureView(footerView);
		footContentHeight = footerView.getMeasuredHeight();
//		footContentWidth = footerView.getMeasuredWidth();
		footerView.setPadding(0, 0, 0, 0);
		footerView.invalidate();
		addFooterView(footerView);
		linearLayoutProgress = (LinearLayout) footerView.findViewById(R.id.progressbar_list_footer);
		textViewFooter = (TextView) footerView.findViewById(R.id.textview_list_footer);
		
	}
	
	public void setActivity(Activity activity) {
		this.activity = activity;
	}

	public void removeFooter() {
		removeFooterView(footerView);
	}

	public void addFooter() {
		addFooterView(footerView);
	}

	public void setFooterVisibility(boolean visibility) {
		if (visibility) {//todo
			footerView.setVisibility(View.VISIBLE);
		} else {
			footerView.setVisibility(View.GONE);
		}
	}
	
	public boolean getFooterVisibility(){
		return footerView.isShown();
	}

	public void removeHeader() {
		removeHeaderView(headView);
	}

	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//		cursorWeibo.lastItem = firstVisibleItem + visibleItemCount - 1;
//        cursorWeibo.totalItemCount = totalItemCount;
		if (firstVisibleItem == 0) {
			tagHead = true;
			pullMood = PULL_MOOD_HEAD;
		} else {
			tagHead = false;
		}
		if(firstVisibleItem+visibleItemCount==totalItemCount){
			if(firstVisibleItem==0&&visibleItemCount==0&&totalItemCount==0){
				return;
			}else{
				tagFoot = true;
				pullMood = PULL_MOOD_FOOT;
			}
		}else{
			tagFoot = false;
		}
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
//			Log.d("fbb", "ACTION_DOWN");
			if (!tagHead && !tagFoot) {
				break;
			}
			if (!isVerticalScrollBarEnabled()) {
				setVerticalScrollBarEnabled(true);
			}
			if (!isRecored) {
				startY = (int) event.getY();
				isRecored = true;
			}
			break;
			
		case MotionEvent.ACTION_MOVE:
			if (!tagHead && !tagFoot) {
				break;
			}
			if (!isVerticalScrollBarEnabled()) {
				setVerticalScrollBarEnabled(false);
			}
			int tempY = (int) event.getY();
			if (!isRecored) {
				isRecored = true;
				startY = tempY;
			} //TODO down没记录的情况下
			int headDistance = (tempY - startY) * 2 / 5;
			int footDistance = (startY-tempY) / 2;
			if(pullMood == PULL_MOOD_FOOT){
				if(footDistance <= 0){
					state = DONE;
					footerView.setPadding(0, 0, 0, 0);
					footerView.invalidate();
				}else if(footDistance > footContentHeight){
					state = RELEASE_To_REFRESH;
					footerView.setPadding(0, 0, 0, footDistance);
					footerView.invalidate();
					textViewFooter.setText("松手加载下一页");
				}else{
					state = PULL_To_REFRESH;
					footerView.setPadding(0, 0, 0, footDistance);
					footerView.invalidate();
					textViewFooter.setText("上拉加载更多");
				}
				
			} else if(pullMood == PULL_MOOD_HEAD){
				if ((headDistance < headContentHeight) && headDistance > 0) {
					state = PULL_To_REFRESH;
					headView.setPadding(0, -headContentHeight + headDistance, 0, 0);
					headView.invalidate();
					
					progressBar.setVisibility(View.GONE);
					tipsTextview.setVisibility(View.VISIBLE);
					lastUpdatedTextView.setVisibility(View.VISIBLE);
					arrowImageView.clearAnimation();
					arrowImageView.setVisibility(View.VISIBLE);
					isFront = true;
					if (isBack) {
						isBack = false;
						arrowImageView.clearAnimation();
						arrowImageView.startAnimation(reverseAnimation);
						tipsTextview.setText("下拉刷新");
					} else {
						tipsTextview.setText("下拉刷新");
					}
					break;
				}else if (headDistance <= 0) {
					state = DONE;
					headView.setPadding(0, -headContentHeight, 0, 0);
					headView.invalidate();
					progressBar.setVisibility(View.GONE);
					arrowImageView.clearAnimation();
					arrowImageView.setImageResource(R.drawable.bg_pull_to_refresh_arrow);
					tipsTextview.setText("下拉刷新");
					lastUpdatedTextView.setVisibility(View.VISIBLE);
				}else{
					state = RELEASE_To_REFRESH;
					headView.setPadding(0, -headContentHeight + headDistance, 0, 0);
					headView.invalidate();
					arrowImageView.setVisibility(View.VISIBLE);
					progressBar.setVisibility(View.GONE);
					tipsTextview.setVisibility(View.VISIBLE);
					lastUpdatedTextView.setVisibility(View.VISIBLE);
					isBack = true;
					if(isFront){
						arrowImageView.clearAnimation();
						arrowImageView.startAnimation(animation);
						isFront = false;
					}
					tipsTextview.setText("松开刷新");
				}
			} else{
			}
			break;

		case MotionEvent.ACTION_UP:
			if (!tagHead && !tagFoot) {
				break;
			}
			tagFoot = false;
			tagHead = false;
			if(pullMood == PULL_MOOD_FOOT){
				if(state == RELEASE_To_REFRESH){
					startFootAnim(300, true);
					textViewFooter.setVisibility(View.GONE);
					linearLayoutProgress.setVisibility(View.VISIBLE);
				}
				if(state == PULL_To_REFRESH){
					startFootAnim(300, false);
				}
			} else if(pullMood == PULL_MOOD_HEAD){
				if (state == PULL_To_REFRESH) {
					state = DONE;
					startHeadAnim(250, headView.getPaddingTop()+headContentHeight, false);
				}
				if (state == RELEASE_To_REFRESH) {
					state = REFRESHING;
					progressBar.setVisibility(View.VISIBLE);
					arrowImageView.clearAnimation();
					arrowImageView.setVisibility(View.INVISIBLE);
					startHeadAnim(250, headView.getPaddingTop(), true);
				}
			}
			isRecored = false;
			isBack = false;

			break;

		}
		return super.onTouchEvent(event);
	}
	
	private void startFootAnim(int during, final boolean isPageDown){
		final int count = during/10;
		final int distance = footerView.getPaddingBottom()/count;
//		Log.d("fbb", "distance:"+distance+" count:"+count);
//		Log.d("fbb", "startFootAnim");
		new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i = 0; i<=count ; i++){
					if(i == count){
						activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								footerView.setPadding(0, 0, 0, 0);
							}
						});
					}else{
						activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								footerView.setPadding(0, 0, 0, footerView.getPaddingBottom()- distance);
							}
						});
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if(isPageDown){
					onPageDown();
				}
			}
		}).start();
	}
	
	private void startHeadAnim(int during, int distance, final boolean isRefresh){
		final int count = during/10;
		final int  perDistance = distance/count;
//		Log.d("fbb", "startHeadAnim");
		new Thread(new Runnable() {
			@Override
			public void run() {
				for(int i = 0; i<=count ; i++){
					if(i == count){
						activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if(isRefresh){
									headView.setPadding(0, 0, 0, 0);
								}else{
									headView.setPadding(0, -headContentHeight, 0, 0);
								}
							}
						});
						
					}else{
						activity.runOnUiThread(new Runnable() {
							@Override
							public void run() {
								headView.setPadding(0, headView.getPaddingTop()- perDistance, 0, 0);
							}
						});
					}
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				if(isRefresh){
					onRefresh();
				}
			}
		}).start();
	}
	
	public void setOnRefreshListener(OnRefreshListener refreshListener) {
		this.refreshListener = refreshListener;
	}
	
	public void setOnPageDownListener(OnPageDownListener pageDownListener) {
		this.pageDownListener = pageDownListener;
	}

	public interface OnRefreshListener {
		public void onRefresh();
	}
	
	public interface OnPageDownListener{
		public void onPageDown();
	}

	public void onRefreshComplete() {
//		Log.d("fbb", "onRefreshComplete");
		state = DONE;
		Date date = new Date();
		int hours = date.getHours();
		int minutes = date.getMinutes();
		int seconds = date.getSeconds();
		String time = hours + ":" + minutes + ":" + seconds;
		lastUpdatedTextView.setText("最近更新:" + time);
//		changeHeaderViewByState();
		if(isRefresh){
			startHeadAnim(200, headContentHeight, false);//TODO 短距离的滑动
			isRefresh = false;
		}
	}
	
	public void onPageDownhComplete() {
		state = DONE;
		textViewFooter.setText("上拉加载更多");
		textViewFooter.setVisibility(View.VISIBLE);
		linearLayoutProgress.setVisibility(View.GONE);
	}

	private void onRefresh() {
		if (refreshListener != null) {
			refreshListener.onRefresh();
			isRefresh = true;
		}
	}
	
	private void onPageDown(){
		if (pageDownListener != null) {
			pageDownListener.onPageDown();
		}
	}
	
	*//**
	 * 设置列表下拉刷新功能不可用
	 *//*
	public void setPullToRefreshInvisible(){
		this.tagHead = false;
	}

	// 此方法直接照搬自网络上的一个下拉刷新的demo，此处是“估计”headView的width以及height
	private void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}
	
	public void onScrollStateChanged(AbsListView view, int scrollState) {
//		if(scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
//			if (view.getId() == R.id.listview_list) {//滑动到最后一条获得更多微博信息
//				if (cursorWeibo.lastItem == cursorWeibo.totalItemCount - 1) {
//					Log.d("fbb", "Idle");
////					pullMood = PULL_MOOD_FOOT;
////					tagFoot = true;
////					reqData(Constants.COMMAND_REQ_LOCAL, Constants.REQ_DATA_PRE_PAGE);
////					listView.setFooterLoading(true);
//				}
//			}
//		}
	}
	
//	@Override
//	public boolean onInterceptTouchEvent(MotionEvent ev){
//		return true;
//	}
}
*/